package com.yazi.mxz.person.controller.firm;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.service.CompanyService;

@Controller("firm-companyController")
@RequestMapping("/firm/company")
public class CompanyController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CompanyController.class);

	@Resource
	CompanyService companyService;

	@RequestMapping("/edit")
	public String edit(Model model) {
		debug(log, "进入企业信息编辑页面");
		model.addAttribute("company", getCompany(true));
		return "firm/company_edit";
	}

	@RequestMapping("/editDo/json")
	@ResponseBody
	public StatusResult editDoJson(Company company) {
		debug(log, "保存企业信息");
		StatusResult jsonResult = new StatusResult();
		try {
			// 防止篡改以下信息
			company.setCodeSeq(null);
			company.setName(null);
			company.setCodeRemain(null);
			company.setStoreRemain(null);
			company.setCatRemain(null);
			company.setMemRemain(null);
			
			companyService.edit(company, getCompany(true));
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "企业信息保存异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}

	@RequestMapping("/barCode/json")
	@ResponseBody
	public ObjectResult<?> barCodeJson() {
		debug(log, "生成企业商品店内码");
		ObjectResult<String> jsonResult = new ObjectResult<String>();
		try {
			jsonResult.setResult(companyService.editCodeSeq(getCompanyId()));
			jsonResult.setOk();
		} catch(MyException e) {
			debug(log, e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "生成企业商品店内码异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/memPromoteDo/json")
	@ResponseBody
	public StatusResult promoteDoJson(int memPromote) {
		debug(log, "更新充值促销开关状态");
		StatusResult jsonResult = new StatusResult();
		try {
			Company company = getCompany(true);
			company.setMemPromote(memPromote);
			companyService.merge(company);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "更新充值促销开关状态异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}

}
